home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / lib / eaccess.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-23  |  3.7 KB  |  167 lines

  1. /* eaccess -- check if effective user id can access file
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie and Torbjorn Granlund */
  19.  
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #ifdef POSIX
  23. #include <unistd.h>
  24. #include <limits.h>
  25. #ifndef NGROUPS_MAX
  26. #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
  27. #endif
  28. #if !defined(sun) && !defined(ultrix)
  29. #define GID_T gid_t
  30. #else
  31. #define GID_T int
  32. #endif
  33. #else
  34. uid_t geteuid ();
  35. gid_t getegid ();
  36. #include <sys/param.h>
  37. #if !defined(NGROUPS_MAX) && defined(NGROUPS)
  38. #define NGROUPS_MAX NGROUPS
  39. #endif
  40. #define GID_T int
  41. #endif
  42.  
  43. #include <errno.h>
  44. #ifndef STDC_HEADERS
  45. extern int errno;
  46. #endif
  47.  
  48. #if defined(EACCES) && !defined(EACCESS)
  49. #define EACCESS EACCES
  50. #endif
  51.  
  52. #ifndef F_OK
  53. #define F_OK 0
  54. #define X_OK 1
  55. #define W_OK 2
  56. #define R_OK 4
  57. #endif
  58.  
  59. int eaccess_stat ();
  60.  
  61. /* The user's effective user id. */
  62. static uid_t euid;
  63.  
  64. /* The user's effective group id. */
  65. static gid_t egid;
  66.  
  67. #ifdef NGROUPS_MAX
  68. char *xmalloc ();
  69. static int in_group ();
  70.  
  71. /* Array of group id's that the user is in. */
  72. static GID_T *groups = 0;
  73.  
  74. /* The number of valid elements in `groups'. */
  75. static int ngroups;
  76. #endif
  77.  
  78. /* Nonzero if the other static variables have valid values. */
  79. static int initialized = 0;
  80.  
  81. /* Return 0 if the user has permission of type MODE on file PATH;
  82.    otherwise, return -1 and set `errno' to EACCESS.
  83.    Like access, except that it uses the effective user and group
  84.    id's instead of the real ones, and it does not check for read-only
  85.    filesystem, text busy, etc. */
  86.  
  87. int
  88. eaccess (path, mode)
  89.      char *path;
  90.      int mode;
  91. {
  92.   struct stat stats;
  93.  
  94.   if (stat (path, &stats))
  95.     return -1;
  96.  
  97.   return eaccess_stat (&stats, mode);
  98. }
  99.  
  100. /* Like eaccess, except that a pointer to a filled-in stat structure
  101.    describing the file is provided instead of a filename. */
  102.  
  103. int
  104. eaccess_stat (statp, mode)
  105.      struct stat *statp;
  106.      int mode;
  107. {
  108.   int granted;
  109.  
  110.   mode &= (X_OK | W_OK | R_OK);    /* Clear any bogus bits. */
  111.  
  112.   if (mode == F_OK)
  113.     return 0;            /* The file exists. */
  114.  
  115.   if (initialized == 0)
  116.     {
  117.       initialized = 1;
  118.       euid = geteuid ();
  119.       egid = getegid ();
  120. #ifdef NGROUPS_MAX
  121.       groups = (GID_T *) xmalloc (NGROUPS_MAX * sizeof (GID_T));
  122.       ngroups = getgroups (NGROUPS_MAX, groups);
  123. #endif
  124.     }
  125.   
  126.   /* The super-user can read and write any file, and execute any file
  127.      that anyone can execute. */
  128.   if (euid == 0 && ((mode & X_OK) == 0 || (statp->st_mode & 0111)))
  129.     return 0;
  130.   if (euid == statp->st_uid)
  131.     granted = (statp->st_mode & (mode << 6)) >> 6;
  132.   else if (egid == statp->st_gid
  133. #ifdef NGROUPS_MAX
  134.        || in_group (statp->st_gid)
  135. #endif
  136.        )
  137.     granted = (statp->st_mode & (mode << 3)) >> 3;
  138.   else
  139.     granted = (statp->st_mode & mode);
  140.   if (granted == mode)
  141.     return 0;
  142.   errno = EACCESS;
  143.   return -1;
  144. }
  145.  
  146. #ifdef NGROUPS_MAX
  147. static int
  148. in_group (gid)
  149.      GID_T gid;
  150. {
  151.   int i;
  152.  
  153.   for (i = 0; i < ngroups; i++)
  154.     if (gid == groups[i])
  155.       return 1;
  156.   return 0;
  157. }
  158. #endif
  159.  
  160. #ifdef TEST
  161. main (argc, argv)
  162.      char **argv;
  163. {
  164.   printf ("%d\n", eaccess (argv[1], atoi (argv[2])));
  165. }
  166. #endif
  167.